package com.boot2.core.utils;

import lombok.extern.apachecommons.CommonsLog;
import okhttp3.*;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * @author zhangweilin
 * @description: xxxxx
 * @date 2018/10/25
 */
@CommonsLog
public abstract class AbstractHttpsClientCall extends AbstractHttpsClientInstance {

    /**
     * @param url
     * @param params
     * @return
     */
    protected static Call createCall(String url, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);
        // 创建一个请求
        log.info("requestUrl:" + requestUrl);
        Request request = new Request.Builder().url(requestUrl).build();
        return getHttpInstance().newCall(request);
    }

    /**
     * @param url
     * @param params
     * @return
     */
    protected static Call createProxyCall(String proxyHost, Integer proxyPort, String url, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);
        // 创建一个请求
        log.info("requestUrl:" + requestUrl);
        Request request = new Request.Builder().url(requestUrl).build();
        return getProxyHttpInstance(proxyHost, proxyPort).newCall(request);
    }

    /**
     * @param url
     * @param headerMap
     * @param params
     * @return
     */
    protected static Call createCall(String url, Map<String, String> headerMap, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);
        // 创建一个请求
        log.info("requestUrl:" + requestUrl);
        Request.Builder builder = new Request.Builder();
        if (null != headerMap) {
            Set<Map.Entry<String, String>> entrySet = headerMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        Request request = builder.url(requestUrl).build();
        return getHttpInstance().newCall(request);
    }

    /**
     * @param url
     * @param headerMap
     * @param params
     * @return
     */
    protected static Call createProxyCall(String proxyHost, Integer proxyPort, String url, Map<String, String> headerMap, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);
        // 创建一个请求
        log.info("requestUrl:" + requestUrl);
        Request.Builder builder = new Request.Builder();
        if (null != headerMap) {
            Set<Map.Entry<String, String>> entrySet = headerMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        Request request = builder.url(requestUrl).build();
        return getProxyHttpInstance(proxyHost, proxyPort).newCall(request);
    }

    /**
     * https请求
     *
     * @param url
     * @param params
     * @return
     */
    protected static Call createCalls(String url, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);
        // 创建一个请求
        Request request = new Request.Builder().url(requestUrl).build();
        return getHttpsInstance().newCall(request);
    }

    /**
     * https请求
     *
     * @param url
     * @param params
     * @return
     */
    protected static Call createProxyCalls(String proxyHost, Integer proxyPort, String url, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);
        // 创建一个请求
        Request request = new Request.Builder().url(requestUrl).build();
        return getProxyHttpsInstance(proxyHost, proxyPort).newCall(request);
    }

    /**
     * okHttp createCall
     *
     * @param url    接口地址
     * @param params 请求参数
     */
    protected static Call createCalls(String url, Map<String, String> headerMap, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);

        // 创建一个请求
        log.info("requestUrl:" + requestUrl);
        Request.Builder builder = new Request.Builder();
        if (null != headerMap) {
            Set<Map.Entry<String, String>> entrySet = headerMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        // 创建一个请求
        Request request = builder.url(requestUrl).build();
        return getHttpsInstance().newCall(request);
    }

    /**
     * okHttp createCall
     *
     * @param url    接口地址
     * @param params 请求参数
     */
    protected static Call createProxyCalls(String proxyHost, Integer proxyPort,String url, Map<String, String> headerMap, Map<String, Object> params) {
        String requestUrl = getFullUrl(url, params);

        // 创建一个请求
        log.info("requestUrl:" + requestUrl);
        Request.Builder builder = new Request.Builder();
        if (null != headerMap) {
            Set<Map.Entry<String, String>> entrySet = headerMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        // 创建一个请求
        Request request = builder.url(requestUrl).build();
        return getProxyHttpsInstance(proxyHost,proxyPort).newCall(request);
    }

    protected static Call getGetCall(String url, Map<String, Object> params) {
        Call call = null;
        if (url.startsWith("http://")) {
            call = createCall(url, params);
        } else if (url.startsWith("https://")) {
            call = createCalls(url, params);
        }
        return call;
    }

    protected static Call getProxyGetCall(String proxyHost, Integer proxyPort, String url, Map<String, Object> params) {
        Call call = null;
        if (url.startsWith("http://")) {
            call = createProxyCall(proxyHost, proxyPort, url, params);
        } else if (url.startsWith("https://")) {
            call = createProxyCalls(proxyHost, proxyPort, url, params);
        }
        return call;
    }


    protected static Call getGetCall(String url, Map<String, String> headerMap, Map<String, Object> params) {
        // 创建请求对象
        Call call = null;
        if (url.startsWith("http://")) {
            call = createCall(url, headerMap, params);
        } else if (url.startsWith("https://")) {
            call = createCalls(url, headerMap, params);
        }
        return call;
    }

    protected static Call getProxyGetCall(String proxyHost, Integer proxyPort, String url, Map<String, String> headerMap, Map<String, Object> params) {
        // 创建请求对象
        Call call = null;
        if (url.startsWith("http://")) {
            call = createProxyCall(proxyHost, proxyPort, url, headerMap, params);
        } else if (url.startsWith("https://")) {
            call = createProxyCalls(proxyHost, proxyPort, url, headerMap, params);
        }
        return call;
    }


    protected static Call getPostsJsonCall(String url, String json, String contentType, Map<String, String> pararmMap, Map<String, String> headerMap) {
        /** 忽略SSL协议证书 */
        OkHttpClient build = getHttpsInstance();
        final RequestBody body = createJsonRequestBodyWithParamMap(contentType, json, pararmMap);
        Request.Builder builder = new Request.Builder();
        if (null != headerMap) {
            Set<Map.Entry<String, String>> entrySet = headerMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        final Request request = builder.url(url).post(body).build();
        return build.newCall(request);
    }

    protected static Call getProxyPostsJsonCall(String proxyHost, Integer proxyPort, String url, String json, String contentType, Map<String, String> pararmMap, Map<String, String> headerMap) {
        /** 忽略SSL协议证书 */
        OkHttpClient build = getProxyHttpsInstance(proxyHost,proxyPort);
        final RequestBody body = createJsonRequestBodyWithParamMap(contentType, json, pararmMap);
        Request.Builder builder = new Request.Builder();
        if (null != headerMap) {
            Set<Map.Entry<String, String>> entrySet = headerMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        final Request request = builder.url(url).post(body).build();
        return build.newCall(request);
    }


    /**
     * 创建json请求
     *
     * @param contentType
     * @param json
     * @return
     */
    protected static RequestBody createJsonRequestBodyWithParamMap(String contentType, String json, Map<String, String> paramMap) {
        MediaType type = MediaType.parse(contentType);
//		RequestBody requestBody = builder.build().create(type, json);
        RequestBody requestBody = RequestBody.create(type, json);
        return requestBody;
    }

    /**
     * @param params
     * @return StringBuilder 返回类型
     * @description 拼接参数
     * @Author liangjilong
     * @Date 2017年7月11日 上午9:34:00
     */
    protected static StringBuilder concatParams(Map<String, ? extends Object> params) {
        StringBuilder builder = new StringBuilder("");// 请求参数为空给一个默认值空字符串
        // 判断是空
        if (null != params) {
            int i = 0;
            for (String key : params.keySet()) {
                Object value = params.get(key);
                if (value instanceof Collection) {
                    Collection collection = (Collection) value;
                    for (Object value1 : collection) {
                        builder.append(i != 0 ? "&" : "");
                        builder.append(key + "[]=" + value1);
                        i++;
                    }
                } else {
                    builder.append(i != 0 ? "&" : "");
                    builder.append(key + "=" + value);
                    i++;
                }

            }
        }
        log.info("组装参数: " + builder.toString());
        return builder;
    }

    /**
     * 拼全路径
     *
     * @param url
     * @param params
     * @return
     */
    public static String getFullUrl(String url, Map<String, Object> params) {
        String requestUrl = url;
        if (null != params) {
            requestUrl = String.format("%s?%s", url, concatParams(params).toString());
        }
        return requestUrl;
    }
}
